package com.k2data.k2app.controller;


import com.k2data.k2app.ResponseConstant;
import com.k2data.k2app.constant.ResponseCode;
import com.k2data.k2app.exception.K2Exception;
import com.k2data.k2app.exception.K2ResponseException;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SendMsgService;
import com.k2data.k2app.validate.SendMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 创蓝的短信接口
 */
@Api(description = "短信接口")
@RestController
@RequestMapping("/sms_cl")
public class SendMsgController {

    private final SendMsgService sendMsgService;

    @Autowired
    public SendMsgController(SendMsgService sendMsgService) {
        this.sendMsgService = sendMsgService;
    }
    /**
     * <p>send message to the mobile number</p>
     * @param sendMessage
     */
    @ApiOperation(value = "发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendMessage", value = "sendMessage", required = true, dataType = "SendMessage", paramType = "body")
    })
    @PostMapping(value = "/send", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> send(@NotNull @Valid @RequestBody SendMessage sendMessage) throws Exception {
        try{
            return ResponseHelper.success(sendMsgService.send(sendMessage));
        }catch (K2Exception e){
            throw new K2ResponseException(ResponseCode.getCode(ResponseCode.TYPE_SERVER_ERROR, ResponseCode.K2ALPHA_MOBILE, ResponseConstant.SEND_ERROR), "the msg send failed",e);
        }
    }
}
